package com.ucar.ui.main;

import com.android.volley.toolbox.ImageLoader;
import com.ucar.AppController;
import com.ucar.R;
import com.ucar.bean.AD;
import com.ucar.ui.about.AboutActivity;
import com.ucar.ui.advise.AdviseActivity;
import com.ucar.ui.base.BaseFragment;
import com.ucar.ui.cheapcode.CheapCodeActivity;
import com.ucar.ui.login.EditPersonDataActivity;
import com.ucar.ui.login.LoginActivity;
import com.ucar.util.DialogUtil;
import com.ucar.util.DialogUtil.OnClickYesListener;
import com.ucar.util.Utils;
import com.ucar.widget.CircleImageView;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuFragment extends BaseFragment implements OnClickListener {
	private TextView tvOrder;
	private TextView tvSuggest;
	private TextView tvAbout;
	private TextView tvQuit;
	private CircleImageView ivUserPic;
	private TextView tvUsername;
	private ImageView ivAd;
	private TextView tv_cheapcode;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_left_menu, container,
				false);
		return view;
	}

	@Override
	public void onInit() {

	}

	@Override
	public void onFindViews() {
		tvOrder = find(R.id.tv_order);
		tvAbout = find(R.id.tv_about);
		tvSuggest = find(R.id.tv_suggest);
		tvQuit = find(R.id.tv_quit);
		ivUserPic = find(R.id.iv_userpic);
		tvUsername = find(R.id.tv_username);
		ivAd = find(R.id.iv_ad);
		tv_cheapcode = find(R.id.tv_cheapcode);
	}

	@Override
	public void onInitViewData() {
		int distance = (int) Utils.dp2px(mContext, 25);

		Drawable orderDra = getResources().getDrawable(R.drawable.icon_order);
		Drawable suggestDra = getResources().getDrawable(
				R.drawable.icon_suggest);
		Drawable aboutDra = getResources().getDrawable(R.drawable.icon_about);
		Drawable quitDra = getResources().getDrawable(R.drawable.icon_quit);

		orderDra.setBounds(0, 0, distance, distance);
		suggestDra.setBounds(0, 0, distance, distance);
		aboutDra.setBounds(0, 0, distance, distance);
		quitDra.setBounds(0, 0, distance, distance);

		tvOrder.setCompoundDrawables(orderDra, null, null, null);
		tvSuggest.setCompoundDrawables(suggestDra, null, null, null);
		tvAbout.setCompoundDrawables(aboutDra, null, null, null);
		tvQuit.setCompoundDrawables(quitDra, null, null, null);
	}

	@Override
	public void onBindListener() {
		tvQuit.setOnClickListener(this);
		ivUserPic.setOnClickListener(this);
		tvSuggest.setOnClickListener(this);
		tvAbout.setOnClickListener(this);
		tv_cheapcode.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_userpic:
			if (!mSpUtil.isLogin()) {
				AppController.getInstance().redirectLogin(mContext);
				return;
			}
			redirectActivity(EditPersonDataActivity.class);
			break;
		case R.id.tv_suggest:
			redirectActivity(AdviseActivity.class);
			break;
		case R.id.tv_about:
			redirectActivity(AboutActivity.class);
			break;
		case R.id.tv_cheapcode:
			redirectActivity(CheapCodeActivity.class);
			break;
		case R.id.tv_quit:
			if (!mSpUtil.isLogin()) {
				AppController.getInstance().redirectLogin(mContext);
				return;
			}
			DialogUtil.createTextQADialog(mContext,
					getString(R.string.confirm_exit), null,
					new OnClickYesListener() {

						@Override
						public void onClickYes() {
							mSpUtil.setToken("");
							mSpUtil.setLogin(false);
							redirectActivity(LoginActivity.class);
							getActivity().finish();
						}
					});
			break;
		case R.id.tv_order:
			if (!mSpUtil.isLogin()) {
				AppController.getInstance().redirectLogin(mContext);
				return;
			}
		}
	}

	/**
	 * 显示广告
	 * 
	 * @param ad
	 */
	public void showAd(AD ad) {
		String url = ad.getCover();
		if (!TextUtils.isEmpty(url)) {
			AppController.getInstance().getImageLoader()
					.get(url, ImageLoader.getImageListener(ivAd, 0, 0));
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mSpUtil.getGender().equals("女")) {
			ivUserPic.setImageResource(R.drawable.icon_userpic_female);
		}else if (mSpUtil.getGender().equals("男")) {
			ivUserPic.setImageResource(R.drawable.icon_userpic_men);
		}
		tvUsername.setText(mSpUtil.getName());
	}
}
